package org.sam.swing.format;

import java.text.ParseException;

import javax.swing.JFormattedTextField.AbstractFormatter;

/**
 * 纬度格式化对象
 * @author sam
 *
 */
public class LatitudeFormatter extends AbstractFormatter {

	private static final long serialVersionUID = 7208251341286746281L;
	
	/**
	 * {@inheritDoc}
	 * 必须输入，否则报错
	 */
	@Override
	public Object stringToValue(String text) throws ParseException {
		if(text == null)
			return null;
		
		if (!text.startsWith("N") && !text.startsWith("S"))
			throw new ParseException("请输入N或S" , 0);
		
		//^-?((0|[1-8]?[0-9]?)(([.][0-9]{1,4})?)|90(([.][0]{1,4})?))$
		
		if (!text.substring(1).matches("[0-9]+(.[0-9]+)?"))
			throw new ParseException("请输入正确的格式" , 1);
		
		return text;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String valueToString(Object value) throws ParseException {
		if(value == null)
			return "";
		
		return value.toString();
	}

}
